#include"DemoApplyForce.h"
#include"LSYEasyBody.h"
#include"LSYEasyWorld.h"
#include"Tool.h"

USING_NS_CC;

bool DemoApplyForce::init()
{
	if (!AbstractBox2DTest::init(b2Vec2(0, -10)))
	{
		return false;
	}
	scheduleOnce(schedule_selector(DemoApplyForce::work), 1);
	return true;
}

void DemoApplyForce::box2DAppReady()
{
	auto size = Director::getInstance()->getWinSize();
	LSYEasyBody::createRectangle(b2Vec2(0, 0), size.width, size.height);

	bird = LSYEasyBody::createCircle(b2Vec2(20, 20), 10);
	birdVelocity = b2Vec2(10, 15);
	bird->SetLinearVelocity(birdVelocity);
	
	force.y = 0;
	force.x = -bird->GetMass() * 32 * 10;
	
	impulse.x = bird->GetMass() * 10;
	impulse.y = bird->GetMass() * 10;
}

void DemoApplyForce::work(float dt)
{
	//bird->ApplyForce(force, bird->GetPosition(), true);
	bird->ApplyLinearImpulse(impulse, bird->GetPosition(), true);
	
	b2Body* bullet = LSYEasyBody::createCircle(b2Vec2(bird->GetPosition().x * 30, bird->GetPosition().y * 30 + 20), 5);
	bullet->ApplyLinearImpulse(b2Vec2(0, -bullet->GetMass() * 20), bullet->GetPosition(), true);
}
